package com.overlord.im.server.message.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.overlord.im.server.message.entity.ServerMessageEntity;
import com.overlord.im.server.message.mapper.ServerMessageMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wuwei
 * @className ServerMessageService
 * @description
 * @date 2020-05-31 01:16
 **/
@Service
public class ServerMessageService extends ServiceImpl<ServerMessageMapper, ServerMessageEntity> {

    public ServerMessageEntity findByClientMessageId(Long clientMessageId, Long clientId) {
        ServerMessageEntity serverMessageEntity = getBaseMapper().selectOne(
                new LambdaQueryWrapper<ServerMessageEntity>().eq(ServerMessageEntity::getClientMessageId,
                        clientMessageId)
                        .eq(ServerMessageEntity::getMsgFrom, clientId));
        return serverMessageEntity;
    }

    public ServerMessageEntity findBySequenceId(Long sequenceId) {
        List<String> columns = Lists.newArrayList("id", "clientMessageId", "msgFrom", "msgTo",
                "sequenceId");
        return getBaseMapper().selectOne(new LambdaQueryWrapper<ServerMessageEntity>().select(
                tableFieldInfo -> columns.contains(tableFieldInfo.getProperty())
        ).eq(ServerMessageEntity::getSequenceId, sequenceId));
    }

}
